<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
    <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyId"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>


    <el-table
      v-loading="loading"
      :data="productList"
    >
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="供应商" align="center" prop="supplyName" width="150"/>
      <el-table-column label="商品码" align="center" prop="productCode" width="150"/>
      <el-table-column label="商品名称" align="center" prop="productName" width="150"/>
        <el-table-column label="规格" align="center" prop="unit" width="80"/>
      <el-table-column
        label="生产日期"
        align="center"
        prop="manufactureDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manufactureDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过期日期"
        align="center"
        prop="expirationDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="categoryName" width="80"/>
         <el-table-column label="建议采价" align="center" prop="cjPrice" width="80"/>
      <el-table-column label="建议销价" align="center" prop="xsPrice" width="80"/>
            <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === '0'">启用</el-tag>
          <el-tag type="danger" v-if="scope.row.status === '1'">禁用</el-tag>
        </template>
      </el-table-column>
       <el-table-column label="审核状态" align="center" prop="isSh" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isSh === '0'">已通过</el-tag>
          <el-tag type="info" v-if="scope.row.isSh === '1'">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.isSh === '2'">未通过</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="产地" align="center" prop="countryOfOrigin" width="100"/>
      <el-table-column label="商品品牌" align="center" prop="productBrand" width="150"/>
     
      <el-table-column label="创建人" align="center" prop="createName" width="80"/>
      <el-table-column
      fixed="right"
      width="200"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
           <el-button
            v-if="scope.row.status === '1'"
            :disabled="scope.row.isSh !== '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQj(scope.row)"
            v-hasPermi="['supermarket:supply:qj']"
            >启用</el-button
          >
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQj(scope.row)"
            v-hasPermi="['supermarket:supply:qj']"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  listProduct,
  qjProduct
} from "@/api/supermarket/product";
import { listCategory } from "@/api/supermarket/category";
import {listSupplyGK} from "@/api/supermarket/supply";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Product",
  dicts: ["sys_normal_disable"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 商品分类表格数据
      categoryOptions: [],
      //供客
      supplyOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: null,
        productName: null,
        productBrand: null,
        cjPrice: null,
        xsPrice: null,
        unit: null,
        manufactureDate: null,
        expirationDate: null,
        countryOfOrigin: null,
        categoryId: null,
        supplyId: null,
        stockId: null,
        isSh: null,
        status: null,
        createName: null,
        updateName: null,
        shRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: "商品码不能为空", trigger: "blur" },
        ],
        productName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        unit: [{ required: true, message: "规格不能为空", trigger: "blur" }],
        manufactureDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" },
        ],
        expirationDate: [
          { required: true, message: "过期日期不能为空", trigger: "blur" },
        ],
        categoryId: [
          { required: true, message: "分类不能为空", trigger: "blur" },
        ],
        supplyId: [
          { required: true, message: "供应商不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      const userName = this.$store.state.user.name
      this.queryParams.createName = userName;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.categoryName,
        children: node.children,
      };
    },
    normalizer1(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.supplyId,
        label: node.supplyName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productCode: null,
        productName: null,
        productBrand: null,
        cjPrice: null,
        xsPrice: null,
        unit: null,
        manufactureDate: null,
        expirationDate: null,
        countryOfOrigin: null,
        categoryId: null,
        supplyId: null,
        stockId: null,
        isSh: null,
        status: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createName: null,
        updateName: null,
        shRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQj(row) {
      qjProduct(row.productId).then(res => {
         this.$modal.msgSuccess("操作成功");
        this.getList();
      })
    }
  },
};
</script>
